package com.ct.gd.passport.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.ct.gd.passport.common.connect.CtServer;
import com.ct.gd.passport.common.constants.Config;
import com.ct.gd.passport.common.constants.Protocol;
import com.fuckhtc.gson.Gson;
import com.fuckhtc.gson.reflect.TypeToken;
import com.up91.common.android.connect.Params;
import com.up91.common.android.exception.NullDataException;

public class TicketInfo implements Serializable{
    public String serial;
    public String terminal;
    public String comment;
    public StartInfo[] start;

    public String getStartInfos() {
        StringBuilder builder = new StringBuilder();
        for (StartInfo info : start) {
            builder.append(info.place).append("\n").append(info.time).append("\n");
        }
        return builder.toString();
    }

    public static class StartInfo implements Serializable{
        public String place;
        public String time;

        public StartInfo(String place, String time) {
            this.place = place;
            this.time = time;
        }
    }

    public static List<TicketInfo> load(int type) {
        Params params = new Params();
        params.put(Protocol.Fields.TICKET_BOOKING_TYPE, type);

        if (Config.ON_LINE) {
            String json = CtServer.getServer().doGet(Protocol.Commands.TICKET_INFO, params);
            try {
                JSONObject jObj = new JSONObject(json);
                TypeToken<List<TicketInfo>> typeToken = new TypeToken<List<TicketInfo>>() {
                };
                return new Gson().fromJson(
                        jObj.getString(Protocol.Fields.DATA),
                        typeToken.getType());
            } catch (JSONException e) {
                e.printStackTrace();
                throw new NullDataException();
            }
        }

        List<TicketInfo> infos = new ArrayList<TicketInfo>();
//        TicketInfo info = null;
//        for (int i = 0; i < 5; i++) {
//            info = new TicketInfo();
//            info.serial = "B0011";
//            info.terminal = "屯门、葵芳、APM观塘、旺角油麻地（碧街）、钻石山、香港国际机场、迪士尼乐园、海洋公园";
//            info.comment = "此班车先停深圳湾再停皇岗;此班车加停迪士尼乐园站、乘客需要前往皇岗口岸过关";
//            info.start = new StartInfo[] {new StartInfo("佛山中旅(佳宁娜大酒店)", "7: 00"), new StartInfo("海外国旅(南海体育馆)", "7: 20")};
//            infos.add(info);
//        }
        return infos;
    }
}
